//
//  MainViewController.swift
//  微博
//
//  Created by Mr.Deng on 15/9/21.
//  Copyright © 2015年 Mr.Deng. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        setupComposedButton()
    
    }
    
    @objc private func composedBtnClick(){
        print("点击了按钮")
    }
    
    //tabbar的按钮在此方法才创建，符合OC懒加载的风格
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
//        print(tabBar.subviews)
        
        tabBar.bringSubviewToFront(composedButton)
    }
    
    private func setupComposedButton(){
        
        let count = self.childViewControllers.count
        let w = tabBar.bounds.width / (CGFloat)(count) - 1
        composedButton.frame = CGRectInset(tabBar.bounds, 2 * w, 0)
        tabBar.addSubview(composedButton)
        composedButton.addTarget(self, action: "composedBtnClick", forControlEvents:  .TouchUpInside)
    }
    
    
    private func addChildViewControllers() {
        tabBar.tintColor = UIColor.orangeColor()
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    //添加子控制器
    private func addChildViewController(vc: UIViewController, title:String, imageName:String) {
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        
        addChildViewController(UINavigationController(rootViewController: vc))
    }
   
    
    //懒加载按钮
    private lazy var composedButton:UIButton = UIButton(imageName: "tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    

}
